#include "removablemedia.h"
#include "ui_removablemedia.h"

RemovableMediaScanner * RemovableMedia::scanner;

RemovableMedia::RemovableMedia(QWidget *parent) :
    KeyControllableMenu(parent),
    ui(new Ui::RemovableMedia)
{
    ui->setupUi(this);
    if(!scanner) {
        scanner=new RemovableMediaScanner();
    }
    connect(scanner,SIGNAL(driveAdded(QString)),this,SLOT(addDrive(QString)));
    connect(scanner,SIGNAL(driveRemoved(QString)),this,SLOT(removeDrive(QString)));
    if(!scanner->isRunning()) {
        scanner->start();
    }
    mediaObject=new Phonon::MediaObject(this);
    Phonon::AudioOutput *audioOutput =new Phonon::AudioOutput(Phonon::VideoCategory, this);
    Phonon::createPath(mediaObject,audioOutput);
    widget= new Phonon::VideoWidget(this);
    layout()->addWidget(widget);
}

RemovableMedia::~RemovableMedia()
{
    delete ui;
}

void RemovableMedia::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void RemovableMedia::addDrive(QString drive) {
    qDebug() <<"found drive "<<drive;
    Button * button = new Button(QIcon(""),drive,this);
    addRow(button);
    layout()->addWidget(button);
}

void RemovableMedia::removeDrive(QString drive) {
    qDebug() <<"removing drive "<<drive;
}

void RemovableMedia::buttonSelected() {
    Phonon::MediaSource * source =new Phonon::MediaSource(Phonon::Dvd,getCurrentButton()->text());


    if(source->type()==source->Disc) {
        mediaObject->setCurrentSource(*source);
        Phonon::createPath(mediaObject,widget);
        mediaObject->play();

    }
}
